package com.calvin.android.serialization;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;
import java.sql.SQLClientInfoException;

public class SQLiteSerialization {

    static SQLiteSerialization mInstance = null;
    Context mContext;
    String mPath;
    SQLiteDatabase mSqLiteDatabase;


    public static SQLiteSerialization shared() {
        return mInstance == null ? (mInstance = new SQLiteSerialization()) : mInstance;
    }

    public SQLiteSerialization init(Context context, String name, boolean isExternal, Runnable run) {
        if (null != context && null != name) {
            mContext = context;
            mPath = isExternal ? mContext.getExternalFilesDir("").getPath() : mContext.getFilesDir().getPath();
            mPath = String.format("%s/%s.db", mPath, name);
            if (!new File(mPath).exists()) {
                run.run();
            }
        }
        return this;
    }

    public boolean begin() {
        mSqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(mPath, null);
        return mSqLiteDatabase.isOpen();
    }

    public void end() {
        if (mSqLiteDatabase.isOpen()) {
            mSqLiteDatabase.close();
        }
    }

    public SQLiteSerialization execute(String sql) {
        if (null != sql && !sql.isEmpty() && mSqLiteDatabase.isOpen()) {
            mSqLiteDatabase.execSQL(sql);
        }
        return this;
    }

    public SQLiteSerialization createTable(Object pojo) {
        return this;
    }

    public SQLiteSerialization alterTable(Object pojo) {
        return this;
    }

    public SQLiteSerialization dropTable(String name) {
        return this;
    }


}

